package com.yiwei.zxp.activity;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yiwei.zxp.R;
import com.yiwei.zxp.adapter.CommissionDetailAdapter;
import com.yiwei.zxp.api.Api;
import com.yiwei.zxp.api.bean.CommissionListBean;
import com.yiwei.zxp.api.bean.ShareGoldExplainBean;
import com.yiwei.zxp.base.BaseActivity;
import com.yiwei.zxp.constant.Const;
import com.yiwei.zxp.constant.UserInfoGlobal;
import com.yiwei.zxp.util.Dialog;
import com.yiwei.zxp.util.Network;
import com.yiwei.zxp.util.RxLifeUtil;
import com.yiwei.zxp.util.ToastUtil;
import com.yiwei.zxp.util.Util;
import com.yiwei.zxp.view.ObservableScrollView;
import com.yiwei.zxp.view.OnLoadeMoreListener;
import com.yiwei.zxp.view.V19FrameLayout;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by xsooy-pc on 17-12-8.
 */

public class MyCommissionActivity extends BaseActivity  {


    @BindView(R.id.btn_back)
    ImageView btnBack;
    @BindView(R.id.txt_title)
    TextView txtTitle;
    @BindView(R.id.txt_right)
    ImageView txtRight;
    @BindView(R.id.layout_title)
    V19FrameLayout layoutTitle;
    @BindView(R.id.txt_balance)
    TextView txtBalance;
    @BindView(R.id.btn_withdraw)
    TextView btnWithdraw;
    @BindView(R.id.list)
    RecyclerView list;
    @BindView(R.id.layout_empty)
    LinearLayout layoutEmpty;
    @BindView(R.id.refresh)
    SwipeRefreshLayout refresh;
//    @BindView(R.id.scrollView)
//    ObservableScrollView scrollView;
    private Unbinder unbiner;
    private CommissionDetailAdapter adapter;

    private OnLoadeMoreListener onLoadeMoreListener = new OnLoadeMoreListener() {
        @Override
        public void onLoadMore(int page) {
            getCommissionList(page);
        }

        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);
            int topRowVerticalPosition =
                    (recyclerView == null || recyclerView.getChildCount() == 0) ? 0 : recyclerView.getChildAt(0).getTop();
            refresh.setEnabled(topRowVerticalPosition >= 0);
        }
    };
//我的佣金
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_commission);
        initBarTouming();
        unbiner = ButterKnife.bind(this);
        init();

    }

    private int currentPage = 1;
    private boolean isOkRefresh = false;

//    @Override
//    public void onScrollChanged(ObservableScrollView scrollView, int x, int y, int oldx, int oldy) {
//        if (y == (scrollView.getChildAt(0).getMeasuredHeight() - scrollView.getMeasuredHeight())) {
//            //底部加载
//            if (isOkRefresh) {
//
//                isOkRefresh = false;
//                currentPage++;
//                getCommissionList(currentPage);
//            }
//        }
//
//    }

    private void init() {
        list.setNestedScrollingEnabled(false);
//        scrollView.setScrollViewListener(this);
        list.addOnScrollListener(onLoadeMoreListener);

        adapter = new CommissionDetailAdapter(this);
        list.setAdapter(adapter);
        refresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                onLoadeMoreListener.setPage(1);
                getCommissionList(1);
            }
        });
        getCommissionList(1);
        getUser();
    }


    public void getUser() {
        if (!Network.checkNetwork(this)) {
            ToastUtil.show(this, "网络连接失败");
            return;
        }
        if (refresh != null) {
            refresh.setRefreshing(true);
        }
        Api.api_service.getUserInfo().compose(RxLifeUtil.NocheckOn(this)).
                doFinally(() -> {
                    try {
                        refresh.setRefreshing(false);
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    if (result.code.equals("00")) {
                        UserInfoGlobal.saveUserInfo(result.datas);
                        txtBalance.setText(UserInfoGlobal.getCommission());
                    } else if (result.code.equals("01")) {

                    }
                });
    }

    public void getExplain(String keyword) {
        if (!Network.checkNetwork(this)) {
            ToastUtil.show(this, "网络连接失败");
            return;
        }

        loadingDialog.show();
        ShareGoldExplainBean bean = new ShareGoldExplainBean();
        bean.keyword = keyword;
        Api.api_service.getShareGoldExplain(bean).compose(RxLifeUtil.checkOn(this)).
                doFinally(() -> {
                    try {
                        loadingDialog.dismiss();
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    Dialog.showPacketTips(this, result.datas.title, result.datas.content);
                });
    }

    public void getCommissionList(int page) {
        if (!Network.checkNetwork(this)) {
            ToastUtil.show(this, "网络连接失败");
            return;
        }

        refresh.setRefreshing(true);
        CommissionListBean bean = new CommissionListBean();
        bean.page = page;
        Api.api_service.getCommssionList(bean).compose(RxLifeUtil.checkOn(this)).
                doFinally(() -> {
                    try {
                        refresh.setRefreshing(false);
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    adapter.add(result.datas, page == 1);
                    if (adapter.getItemCount() == 0) {
                        layoutEmpty.setVisibility(View.VISIBLE);
                    } else {
                        layoutEmpty.setVisibility(View.GONE);
                    }
                });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbiner.unbind();
        unbiner = null;
    }

    @OnClick({R.id.btn_back, R.id.txt_right, R.id.btn_withdraw})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_back:
                finish();
                break;
            case R.id.txt_right:
                getExplain(Const.KeyWord.COMMISSION);
                break;
            case R.id.btn_withdraw:
                if (Double.valueOf(UserInfoGlobal.getCommission()) < 100) {
                    Dialog.showDefaultDialog(this, "", "佣金余额必须满100元才能提现喔！", "", "我知道了", false, new Dialog.DialogDefaultClickListener() {
                        @Override
                        public void confirm() {

                        }

                        @Override
                        public void cancel() {

                        }
                    });
                    return;
                }
                Util.startActivity(this, WithdrawActivity.class);
                break;
        }
    }
}
